/* GUI CONVERTED from adadcgf.p (converter v1.76) Mon Apr 21 19:43:34 2003 */
/* adadcgf.p - CHANGE ADDRESS CODE subroutine                                */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                       */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* Revision: $                                                          */
/*V8:ConvertMode=Maintenance                                                 */
/* REVISION: 7.3      Created:       06/03/94   BY: jzs *GM54*               */
/* REVISION: 7.3      LAST MODIFIED: 10/26/94   BY: ame *FS85*               */
/* REVISION: 8.5      LAST MODIFIED: 01/30/96   BY: *J0CV* Robert Wachowicz  */
/* REVISION: 8.6      LAST MODIFIED: 10/04/96   BY: *K003* Steve Goeke       */
/* REVISION: 8.6      LAST MODIFIED: 11/19/96   BY: *H0PD* Aruna Patil       */
/* REVISION: 8.6      LAST MODIFIED: 03/04/97   BY: *H0T5* Ajit Deodhar      */

/*! GM54 split out this code from adadcg.p, the parent program. This is
    the processing done during a merge.
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.0      LAST MODIFIED: 03/10/99   BY: *M0B3* Michael Amaladhas  */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 05/11/00   BY: *L0XM* Sandeep Rao        */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* jyn                */
/* REVISION: 9.1      LAST MODIFIED: 08/23/00   BY: *L13J* Rajesh Kini        */
/* REVISION: 9.1      LAST MODIFIED: 02/06/01   BY: *N0VG* Jyoti Thatte       */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision:     BY: Subashini Bala        DATE: 11/15/01  ECO: *M1PY*  */
/* $Revision: 1.9.1.12 $    BY: Narathip W.           DATE: 04/17/03  ECO: *P0Q4*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
*/

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */
{cxcustom.i "ADADCGF.P"}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE adadcgf_p_1 "Records Updated"
/* MaxLen: Comment: */

&SCOPED-DEFINE adadcgf_p_2 "Data Set"
/* MaxLen: Comment: */

&SCOPED-DEFINE adadcgf_p_3 "Old Address"
/* MaxLen: Comment: */

&SCOPED-DEFINE adadcgf_p_4 "New Address"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define shared variable addr1     like ad_addr label {&adadcgf_p_3}.
define shared variable addr2     like ad_addr label {&adadcgf_p_4}.
define shared variable type1     like ad_type.
define shared variable custend1  like mfc_logical.

define variable balance          like cm_balance.
define variable num_rec          as integer no-undo.
define variable i                as integer no-undo.
define shared variable cnt_clear like mfc_logical.

define variable merge_cust       like cph_cust.
define variable merge_ship       like cph_ship.

define buffer cmhhist            for cmh_hist.
define buffer cphhist            for cph_hist.
define buffer sphhist            for sph_hist.
define buffer cpmstr             for cp_mstr.
define buffer cddet1             for cd_det.
define buffer cddet2             for cd_det.
define buffer eumstr             for eu_mstr.
define buffer euddet             for eud_det.
define buffer armstr             for ar_mstr.
define buffer sgaddet            for sgad_det.

{gpfilev.i}

define shared frame b.
{&ADADCGF-P-TAG1}
FORM /*GUI*/    /* STANDARD CODE CHANGE FORM */
   file_desc      label {&adadcgf_p_2} format "x(30)"
   num_rec        label {&adadcgf_p_1}
with frame b width 80 down no-attr-space THREE-D /*GUI*/.

{&ADADCGF-P-TAG2}

/* SET EXTERNAL LABELS */
setFrameLabels(frame b:handle).

/* ALLOW CUSTOMER AND ENDUSER MERGE. */
if type1 = "customer"
or type1 = "enduser"
or type1 = "ship-to"
or custend1
then do:

   /* CHANGED cmh_hist LOOP SO ONLY ONE PASS IS REQUIRED */
   num_rec = 0.

   for each cmh_hist exclusive-lock
      where cmh_cust = addr1:
      find cmhhist
         where cmhhist.cmh_cust = addr2
         and cmhhist.cmh_year   = cmh_hist.cmh_year no-error.
      if available cmhhist
      then do:
         assign
            cmhhist.cmh_tot_cost = cmhhist.cmh_tot_cost
                                 + cmh_hist.cmh_tot_cost
            cmhhist.cmh_tot_sale = cmhhist.cmh_tot_sale
                                 + cmh_hist.cmh_tot_sale.
         delete cmh_hist.
      end.  /* IF AVAILABLE cmhhist */
      else
         cmh_hist.cmh_cust = addr2.
      num_rec = num_rec + 1.
   end.  /* FOR EACH cmh_hist ... */

   /* DISPLAY STATUS IN FRAME b */
   {mfcscgb.i cmh_hist}

   /* CUSTOMER-ITEM HISTORY*/

   hide message no-pause.
   num_rec = 0.

   for each cph_hist
       where cph_pl >= ""
       and cph_part >= ""
       and cph_type >= ""
       and cph_year >= 0
       exclusive-lock:

      if cph_cust = addr1
      or cph_ship = addr1
      then do:
         if cph_cust = addr1
         then
            merge_cust = addr2.
         else
            merge_cust = cph_cust.
         if cph_ship = addr1
         then
            merge_ship = addr2.
         else
            merge_ship = cph_ship.
         find cphhist
            where cphhist.cph_cust = merge_cust
            and cphhist.cph_ship = merge_ship
            and cphhist.cph_pl   = cph_hist.cph_pl
            and cphhist.cph_part = cph_hist.cph_part
            and cphhist.cph_type = cph_hist.cph_type
            and cphhist.cph_year = cph_hist.cph_year
            and cphhist.cph_site = cph_hist.cph_site
            exclusive-lock no-error.
         if not available cphhist
         then do:
            if cph_hist.cph_cust = addr1
            then
               cph_hist.cph_cust = addr2.
            if cph_hist.cph_ship = addr1
            then
               cph_hist.cph_ship = addr2.
         end.  /* IF NOT AVAILABLE cphhist */
         else do:
            do i = 1 to 12:
               assign
                  cphhist.cph_qty[i]   = cphhist.cph_qty[i]
                                       + cph_hist.cph_qty[i]
                  cphhist.cph_sales[i] = cphhist.cph_sales[i]
                                       + cph_hist.cph_sales[i]
                  cphhist.cph_cost[i]  = cphhist.cph_cost[i]
                                       + cph_hist.cph_cost[i].
            end.  /* DO i = 1 TO 12 */
            assign
               cphhist.cph_tot_qty    = cphhist.cph_tot_qty
                                      + cph_hist.cph_tot_qty
               cphhist.cph_tot_sale   = cphhist.cph_tot_sale
                                      + cph_hist.cph_tot_sale
               cphhist.cph_tot_cost   = cphhist.cph_tot_cost
                                      + cph_hist.cph_tot_cost.
            delete cph_hist.
         end.  /* ELSE DO */
         num_rec = num_rec + 1.
      end. /* IF cph_cust ... */
   end.  /* FOR EACH cph_hist */

   {mfcscgb.i cph_hist}   /* DISPLAY STATUS IN FRAME b */

   do transaction:
      find cm_mstr
         where cm_addr = addr1
         exclusive-lock
         no-error.
      if available cm_mstr
      then do:
         balance = cm_balance.
         delete cm_mstr.
      end. /* IF AVAILABLE cm_mstr */
      find cm_mstr
         where cm_addr = addr2
         exclusive-lock
         no-error.
      if available cm_mstr
      then do:
         cm_balance = cm_balance + balance.
         release cm_mstr.
      end. /* IF AVAILABLE cm_mstr */
   end. /* DO TRANSACTION */

   /* SHIP-TO REFERENCES */
   {mfcscg.i ad_mstr ad_ref addr1 addr2}

   /* DELETE DUPLICATE CUST PARTS TO AVOID UNIQ INDEX ERROR*/
   for each cp_mstr
      where cp_cust = addr2:
      find cpmstr
         where cpmstr.cp_cust    = addr1
         and cpmstr.cp_cust_part = cp_mstr.cp_cust_part
         exclusive-lock
         no-error.
      if available cpmstr
      then
         delete cpmstr.
   end. /* FOR EACH cp_mstr ... */

end. /* CUSTOMER OR ENDUSER */

/* SUPPLIER */
if type1 = "supplier"
then do:
   do transaction:
      find vd_mstr
         where vd_addr = addr1.
      balance = vd_balance.
      delete vd_mstr.

      for each ers_mstr
         where ers_vend = addr1 exclusive-lock:
         delete ers_mstr.
      end.  /* FOR EACH ers_mstr ... */

      find vd_mstr
         where vd_addr = addr2.
      vd_balance = vd_balance + balance.
      release vd_mstr.
   end.  /* DO TRANSACTION */

   /*CHANGE ad_ref ON REMIT-TO*/
   {mfcscg.i ad_mstr ad_ref addr1 addr2}

end. /* IF type1 = "supplier" */

/* SALESPERSON */
if type1 = "slsprsn"
then do:
   for each sph_hist
      where sph_slspsn1 = addr2:
      find sphhist
         where sphhist.sph_slspsn1 = addr1
         and sphhist.sph_year      = sph_hist.sph_year
         exclusive-lock no-error.
      if available sphhist
      then do:
         assign
            sph_hist.sph_tot_cost = sphhist.sph_tot_cost
                                  + sph_hist.sph_tot_cost
            sph_hist.sph_tot_sale = sphhist.sph_tot_sale
                                  + sph_hist.sph_tot_sale.
         do i = 1 to 12:
            assign
               sph_hist.sph_cost[i]  = sphhist.sph_cost[i]
                                     + sph_hist.sph_cost[i]
               sph_hist.sph_quota[i] = sphhist.sph_quota[i]
                                     + sph_hist.sph_quota[i]
               sph_hist.sph_sales[i] = sphhist.sph_sales[i]
                                     + sph_hist.sph_sales[i].
         end.  /* DO i 1 TO 12 */
         delete sphhist.
      end.  /* IF AVAILABLE sphhist */
   end.  /* FOR EACH sph_hist */

   {mfcscg.i sph_hist sph_hist.sph_slspsn1 addr1 addr2}

   /* DELETE THE OLD SALESPERSON RECORD */
   do transaction:
      find sp_mstr where sp_addr = addr1.
      delete sp_mstr.
   end.  /*  DO TRANSACTION */

   for each spd_det exclusive-lock
      where spd_addr = addr1:
      define buffer spddet for spd_det.
      if not can-find(spddet where spddet.spd_addr = addr2
                      and spddet.spd_prod_ln       = spd_det.spd_prod_ln
                      and spddet.spd_part          = spd_det.spd_part
                      and spddet.spd_cust          = spd_det.spd_cust)
      then
         spd_det.spd_addr = addr2.
   end.  /* FOR EACH  spd_det ... */
end. /* SALESPERSON */

/* ENDUSER */
if can-find (ls_mstr where ls_addr = addr1
             and ls_type = "enduser")
             or can-find (first eu_mstr where eu_cm_nbr = addr1)
then do:
   num_rec = 0.

   for each eu_mstr
      where eu_addr = addr1
      or eu_cm_nbr  = addr1
      exclusive-lock:
      find eumstr
         where eumstr.eu_addr = addr2
         and eumstr.eu_cm_nbr = addr2 no-error.
      if available eumstr
      then do:
         if eu_mstr.eu_addr    = addr1
         and eu_mstr.eu_cm_nbr = addr1
         then do:
            eumstr.eu_po_accum = eumstr.eu_po_accum
                                 + eu_mstr.eu_po_accum.
            delete eu_mstr.
         end.  /* IF eu_mstr.eu_addr ...  */
         else
            eu_mstr.eu_cm_nbr = addr2.
      end.  /* IF AVAILABLE eu_mstr */
      else do:
         if eu_mstr.eu_addr    = addr1
         and eu_mstr.eu_cm_nbr = addr1
         then
            assign
               eu_mstr.eu_addr   = addr2
               eu_mstr.eu_cm_nbr = addr2.
         else
            eu_mstr.eu_cm_nbr = addr2.
      end.  /* ELSE DO */
      num_rec = num_rec + 1.
   end.  /*  FOR EACH eu_mstr ... */

   /* DISPLAY STATUS IN FRAME b */
   {mfcscgb.i eu_mstr}

   num_rec = 0.

   for each eud_det exclusive-lock
      where eud_addr = addr1:
      find euddet
         where euddet.eud_addr  = addr2
         and euddet.eud_ls_type = eud_det.eud_ls_type
         and euddet.eud_sort    = eud_det.eud_sort
         no-lock no-error.
      if not available euddet
      then
         eud_det.eud_addr = addr2.
      else
         delete eud_det.
      num_rec = num_rec + 1.
   end.  /* FOR EACH eud_det ... */

   /* DISPLAY STATUS IN FRAME b */
   {mfcscgb.i eud_det}

 end.  /* IF CAN-FIND ... */

/* ADDED cd_det MASTER COMMENTS. */
num_rec = 0.

for each cddet1 exclusive-lock
   where cddet1.cd_ref =  addr1:
   /* DETERMINE IF COMMENT ALREADY EXISTS FOR addr2 */
   find cddet2
      where cddet2.cd_ref = addr2
      and cddet2.cd_type = cddet1.cd_type
      and cddet2.cd_lang = cddet1.cd_lang
      and cddet2.cd_seq  = cddet1.cd_seq no-lock no-error.
   /* CREATE NEW COMMENT AFTER LAST EXISTING COMMENT. */
   if available cddet2
   then do:
      find last cddet2
         where cddet2.cd_ref = cddet1.cd_ref
         and cddet2.cd_type = cddet1.cd_type
         and cddet2.cd_lang = cddet1.cd_lang no-lock no-error.
      create cd_det.
      assign
         cd_det.cd_ref  = cddet1.cd_ref
         cd_det.cd_type = cddet1.cd_type
         cd_det.cd_lang = cddet1.cd_lang
         cd_det.cd_seq  = cddet2.cd_seq + 1.
      do i = 1 to 15:
         cd_det.cd_cmmt[i] = cddet1.cd_cmmt[i].
      end.  /* DO i = 1 TO 15  */
      delete cddet1.
   end.  /* IF AVAILABLE cddet2 */
   else
      cddet1.cd_ref = addr2.
   num_rec = num_rec + 1.
end. /* FOR EACH cd_det. */

/* PREVENT DUPLICATE ADDRESSES WITHIN A SHIPPING GROUP BY DELETING */

for each sgad_det no-lock
   where sgad_addr = addr2:
   find sgaddet exclusive-lock
      where sgaddet.sgad_addr   = addr1
      and sgaddet.sgad_is_src = sgad_det.sgad_is_src
      and sgaddet.sgad_grp    = sgad_det.sgad_grp
      no-error.
   if available sgaddet
   then
      delete sgaddet.
end.  /* FOR EACH sgad_det ... */

/* WHILE MERGING; DELETE (csbd_det) BANK DETAIL RECORDS HAVING OLD ADDRESS */
for each csbd_det exclusive-lock
   where csbd_addr = addr1:
   delete csbd_det.
end. /* FOR EACH csbd_det */

/* END OF FILE adadagf.p */
